package trinidad;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

public class NetPlayer extends Thread implements ArtificalPlayer {
	
	GamePlay play;
	ServerSocket serverSocket;
	private String ip;
	
	public NetPlayer(GamePlay play){
		super();
		this.play = play;
		this.start();
	}
	
	public void run(){
		
	}
	
	public Move[] makeMove(GameState state, int player) {
		
		System.out.println("Sieciowy oponent cos robi");
		//System.out.println(state.getString());
		System.out.println("Gracz nr"+player);
		if(play.getTurn()==1){
			System.out.println("Klient 1: Odbieram");
			setServerSocket("5555");
			System.out.println("slucham na porcie: 5555");
			GameState d = (GameState)this.receive(state,this.ip,"4444");
			//System.out.println(d.getString());
			play.setCurrentState(d);

		} else {
			System.out.println("Klient 0: Odbieram");
			setServerSocket("4444");
			System.out.println("slucham na porcie: 4444");
			GameState d = (GameState)this.receive(state,this.ip,"5555");
			System.out.println(d.getString());
			play.setCurrentState(d);
		}
		//return pl.makeMove(state, player);
		return new Move[0];
	}

	public Object send(Object what, String ip, String port){
		
		//setServerSocket(port);
		System.out.println("Wysylam");
		/*
		NetThread n = new NetThread(what,serverSocket);
		n.start();
		try {
			n.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
		Socket clientSocket = getClientSocket(serverSocket);
		
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			oos.writeObject(what);
			
			//ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
			
			//Object dostal = ois.readObject();
			
			oos.close();
			//ois.close();
			
			clientSocket.close();
			//serverSocket.close();
			//return dostal;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("Wyslalem");
		return null;
	}

	public Object receive(Object what, String ip, String port){
		
		Socket clientSocket;
		try {
			System.out.println("ip:"+ip+"; port:"+port);
			clientSocket = new Socket(ip, Integer.parseInt(port));
			
			//ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
			ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
			
			Object dostal = ois.readObject();
			//oos.writeObject(what);
			
			//oos.close();
			ois.close();
			
			clientSocket.close();
			return dostal;
			
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e){
			System.out.println("brak połączenia");
		
			JOptionPane.showMessageDialog(null,"Brak połączenia","Sieć",JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
		return null;

	}

	public ServerSocket setServerSocket(String port) {
		try {
			int port2 = Integer.parseInt(port);
			if(serverSocket == null)
				serverSocket = new ServerSocket(port2);
			return serverSocket;
		} catch (IOException e) {
			System.err.println("Could not listen on port: "+port);
			return null;
		}
	}
	
	private Socket getClientSocket(ServerSocket s) {
		try {
			return s.accept();
		} catch (IOException e) {
			System.err.println("Accept failed.");
			return null;
		}
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getStateValue(GameState state, int player) {
		// TODO Auto-generated method stub
		return 0;
	}
}
